package com.nursinghome.management.controller;

import com.nursinghome.management.model.*;
import com.nursinghome.management.service.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/admin")
public class AdminController {

    private final UserService userService;
    private final AdminService adminService;
    private final StaffService staffService;
    private final ElderlyService elderlyService;
    private final HealthRecordService healthRecordService;
    private final MedicationService medicationService;

    public AdminController(UserService userService, AdminService adminService, StaffService staffService,
                           ElderlyService elderlyService, HealthRecordService healthRecordService,
                           MedicationService medicationService) {
        this.userService = userService;
        this.adminService = adminService;
        this.staffService = staffService;
        this.elderlyService = elderlyService;
        this.healthRecordService = healthRecordService;
        this.medicationService = medicationService;
    }

    @GetMapping("/dashboard")
    public String dashboard(Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String username = auth.getName();

        // Get counts for dashboard
        List<User> users = userService.getAllUsers();
        List<Staff> staffList = staffService.getAllStaff();
        List<Elderly> elderlyList = elderlyService.getAllElderly();

        model.addAttribute("userCount", users.size());
        model.addAttribute("staffCount", staffList.size());
        model.addAttribute("elderlyCount", elderlyList.size());

        return "admin/dashboard";
    }

    @GetMapping("/users")
    public String listUsers(Model model) {
        List<User> users = userService.getAllUsers();
        model.addAttribute("users", users);
        return "admin/users";
    }

    @GetMapping("/users/add")
    public String showAddUserForm(Model model) {
        model.addAttribute("user", new User());
        return "admin/add-user";
    }

    @PostMapping("/users/add")
    public String addUser(@ModelAttribute("user") User user, @RequestParam String role) {
        Set<String> roles = new HashSet<>();
        roles.add(role);
        userService.registerUser(user, roles);
        return "redirect:/admin/users";
    }

    @GetMapping("/users/edit/{id}")
    public String showEditUserForm(@PathVariable Long id, Model model) {
        User user = userService.getUserById(id);
        model.addAttribute("user", user);
        return "admin/edit-user";
    }

    @PostMapping("/users/edit")
    public String updateUser(@ModelAttribute("user") User user) {
        userService.updateUser(user);
        return "redirect:/admin/users";
    }

    @GetMapping("/users/delete/{id}")
    public String deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return "redirect:/admin/users";
    }

    @GetMapping("/staff")
    public String listStaff(Model model) {
        List<Staff> staffList = staffService.getAllStaff();
        model.addAttribute("staffList", staffList);
        return "admin/staff";
    }

    @GetMapping("/staff/add")
    public String showAddStaffForm(Model model) {
        model.addAttribute("staff", new Staff());
        model.addAttribute("users", userService.getAllUsers());
        return "admin/add-staff";
    }

    @PostMapping("/staff/add")
    public String addStaff(@ModelAttribute("staff") Staff staff) {
        staffService.createStaff(staff);
        return "redirect:/admin/staff";
    }

    @GetMapping("/elderly")
    public String listElderly(Model model) {
        List<Elderly> elderlyList = elderlyService.getAllElderly();
        model.addAttribute("elderlyList", elderlyList);
        return "admin/elderly";
    }

    @GetMapping("/elderly/add")
    public String showAddElderlyForm(Model model) {
        model.addAttribute("elderly", new Elderly());
        model.addAttribute("users", userService.getAllUsers());
        return "admin/add-elderly";
    }

    @PostMapping("/elderly/add")
    public String addElderly(@ModelAttribute("elderly") Elderly elderly) {
        elderlyService.createElderly(elderly);
        return "redirect:/admin/elderly";
    }

    @GetMapping("/health-records")
    public String listHealthRecords(Model model) {
        List<HealthRecord> healthRecords = healthRecordService.getAllHealthRecords();
        model.addAttribute("healthRecords", healthRecords);
        return "admin/health-records";
    }

    @GetMapping("/medications")
    public String listMedications(Model model) {
        List<Medication> medications = medicationService.getAllMedications();
        model.addAttribute("medications", medications);
        return "admin/medications";
    }
}